#include "mesureresultpagecharts.h"

MesureResultPageCharts::MesureResultPageCharts()
{
    Series_HRV = new QLineSeries;
    Series_HRV_UP = new QLineSeries;
    Series_HRV_Down = new QLineSeries;

    Series_psd = new QLineSeries;
    Series_ppg = new QLineSeries;
    Series_appg = new QLineSeries;
    InitialAxis_Chart_HRV();
    InitialAxis_Chart_PSD();
    InitialAxis_Chart_PPG();
    InitialAxis_Chart_aPPG();
}
MesureResultPageCharts::~MesureResultPageCharts()
{
    delete Series_HRV;
    delete Series_HRV_UP;
    delete Series_HRV_Down;
    delete Series_psd;
    delete xAxis_HRV;
    delete yAxis_HRV;
    delete xAxis_psd;
    delete yAxis_psd;
    delete Series_ppg;
    delete Series_appg;
    delete xAxis_ppg;
    delete xAxis_appg;
}

void MesureResultPageCharts::InitialAxis_Chart_HRV()
{
    xAxis_HRV = new QValueAxis();
    yAxis_HRV = new QValueAxis();
    yAxis_HRV->setRange(300, 1500);
    xAxis_HRV->setRange(0, 360);
    xAxis_HRV->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_HRV->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_HRV->setTitleText("ms");
    yAxis_HRV->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_HRV->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_HRV->setGridLineVisible(false);
    yAxis_HRV->setGridLineVisible(true);
    yAxis_HRV->setGridLineColor(QColor(0xC4,0x91,0x44,0x20));
    xAxis_HRV->setLineVisible(false);
    xAxis_HRV->setVisible(false);
    yAxis_HRV->setVisible(true);
}

QChart* MesureResultPageCharts::CreateLineChart_HRV()
{
    QChart *chart = new QChart();
    Series_HRV->setPen(QPen(QColor(0x00,0x00,0x00),2));//设置波形颜色
    Series_HRV_UP->setPen(QPen(QColor(0x33,0xFF,0x0F),1));
    Series_HRV_Down->setPen(QPen(QColor(0x88,0xFF,0x0F),1));
    chart->addSeries(Series_HRV);
    chart->addSeries(Series_HRV_UP);
    chart->addSeries(Series_HRV_Down);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_HRV, Qt::AlignBottom);
    chart->addAxis(yAxis_HRV, Qt::AlignLeft);
    Series_HRV->attachAxis(xAxis_HRV);
    Series_HRV->attachAxis(yAxis_HRV);

    Series_HRV_UP->attachAxis(xAxis_HRV);
    Series_HRV_UP->attachAxis(yAxis_HRV);

    Series_HRV_Down->attachAxis(xAxis_HRV);
    Series_HRV_Down->attachAxis(yAxis_HRV);
    return chart;
}

void MesureResultPageCharts::setHRVData(QList<int>rr)
{

    Series_HRV->clear();
    Series_HRV_UP->clear();
    Series_HRV_Down->clear();
    int rr_Len=rr.size();
    xAxis_HRV->setRange(0, rr_Len);
    double sum=0;
    for(int i=0;i<rr_Len;i++)
    {
        Series_HRV->append(QPointF(i,rr.at(i)));
        sum+=rr.at(i);
    }
    double rr_mean=sum/rr_Len;
    double rr_square_sum=0;
    for(int i=0;i<rr_Len;i++)
    {
        rr_square_sum += (rr.at(i) - rr_mean) * (rr.at(i) - rr_mean) / rr_Len;
    }
    rr_square_sum=std::sqrt(rr_square_sum);
    for(int i=0;i<rr_Len;i++)
    {
        Series_HRV_Down->append(QPointF(i,rr_mean-2*rr_square_sum));
        Series_HRV_UP->append(QPointF(i,rr_mean+2*rr_square_sum));
    }
}

void MesureResultPageCharts::InitialAxis_Chart_PSD()
{
    xAxis_psd = new QValueAxis();
    yAxis_psd = new QValueAxis();
    yAxis_psd->setRange(0, 1);

    xAxis_psd->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_psd->setLabelsFont( QFont("微软雅黑", 10, QFont::Normal, false));
    yAxis_psd->setTitleText("mv^2");
    yAxis_psd->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_psd->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_psd->setGridLineVisible(false);
    yAxis_psd->setGridLineVisible(true);
    yAxis_psd->setGridLineColor(QColor(0xC4,0x91,0x44,0x20));
    xAxis_psd->setLineVisible(false);
    xAxis_psd->setVisible(false);
    yAxis_psd->setVisible(true);
}

QChart* MesureResultPageCharts::CreateLineChart_PSD()
{
    QChart *chart = new QChart();
    Series_psd->setPen(QPen(QColor(0x00,0x00,0x00),2));//设置波形颜色
    chart->addSeries(Series_psd);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_psd, Qt::AlignBottom);
    chart->addAxis(yAxis_psd, Qt::AlignLeft);
    Series_psd->attachAxis(xAxis_psd);
    Series_psd->attachAxis(yAxis_psd);
    return chart;
}
void MesureResultPageCharts::setPSDData(QList<double>f,QList<double>psd)
{
    Series_psd->clear();
    double maxv=psd.at(0);
    for(int i=0;i<128;i++)
    {
        if(maxv<psd.at(i))
            maxv=psd.at(i);
    }
    yAxis_psd->setRange(0, (int)maxv+10);
    for(int i=0;i<128;i++)
    {
        Series_psd->append(QPointF(f.at(i),psd.at(i)));
    }

}


void MesureResultPageCharts::InitialAxis_Chart_PPG()
{
    xAxis_ppg = new QValueAxis();
    yAxis_ppg = new QValueAxis();

    xAxis_ppg->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_ppg->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_ppg->setTitleText("ms");
    yAxis_ppg->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_ppg->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_ppg->setGridLineVisible(false);
    yAxis_ppg->setGridLineVisible(true);
    yAxis_ppg->setGridLineColor(QColor(0xC4,0x91,0x44,0x20));
    xAxis_ppg->setLineVisible(false);
    xAxis_ppg->setVisible(false);
    yAxis_ppg->setVisible(false);
}

QChart* MesureResultPageCharts::CreateLineChart_PPG()
{
    QChart *chart = new QChart();
    Series_ppg->setPen(QPen(QColor(0x00,0x00,0x00),2));//设置波形颜色
    chart->addSeries(Series_ppg);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_ppg, Qt::AlignBottom);
    chart->addAxis(yAxis_ppg, Qt::AlignLeft);
    Series_ppg->attachAxis(xAxis_ppg);
    Series_ppg->attachAxis(yAxis_ppg);
    return chart;
}

void MesureResultPageCharts::setPPGData(QList<double>ppg)
{
    int ppg_Len=ppg.size();
    xAxis_ppg->setRange(0, ppg_Len);
    double minv=ppg.at(0);
    double maxv=ppg.at(0);
    for(int i=0;i<ppg_Len;i++)
    {
        if(minv>ppg.at(i))
            minv=ppg.at(i);
        if(maxv<ppg.at(i))
            maxv=ppg.at(i);
    }
    qDebug()<<"ppg-minv="<<minv;
    qDebug()<<"ppg-maxv="<<maxv;
    yAxis_ppg->setRange(minv,maxv);
    xAxis_ppg->setRange(0, ppg_Len);
    Series_ppg->clear();
    for(int i=0;i<ppg_Len;i++)
    {
        Series_ppg->append(QPointF(i,ppg.at(i)));
    }
}
void MesureResultPageCharts::InitialAxis_Chart_aPPG()
{
    xAxis_appg = new QValueAxis();
    yAxis_appg = new QValueAxis();

    xAxis_appg->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_appg->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_appg->setTitleText("ms");
    yAxis_appg->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_appg->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_appg->setGridLineVisible(false);
    yAxis_appg->setGridLineVisible(true);
    yAxis_appg->setGridLineColor(QColor(0xC4,0x91,0x44,0x20));
    xAxis_appg->setLineVisible(false);
    xAxis_appg->setVisible(false);
    yAxis_appg->setVisible(false);
}

QChart* MesureResultPageCharts::CreateLineChart_aPPG()
{
    QChart *chart = new QChart();
    Series_appg->setPen(QPen(QColor(0x00,0x00,0x00),2));//设置波形颜色
    chart->addSeries(Series_appg);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_appg, Qt::AlignBottom);
    chart->addAxis(yAxis_appg, Qt::AlignLeft);
    Series_appg->attachAxis(xAxis_appg);
    Series_appg->attachAxis(yAxis_appg);
    return chart;
}

void MesureResultPageCharts::setaPPGData(QList<double>appg)
{
    int ppg_Len=appg.size();
    xAxis_appg->setRange(0, ppg_Len);
    double minv=appg.at(0);
    double maxv=appg.at(0);
    for(int i=0;i<ppg_Len;i++)
    {
        if(minv>appg.at(i))
            minv=appg.at(i);
        if(maxv<appg.at(i))
            maxv=appg.at(i);
    }
    qDebug()<<"appg-minv="<<minv;
    qDebug()<<"appg-maxv="<<maxv;
    yAxis_appg->setRange(minv,maxv);
    xAxis_appg->setRange(0,ppg_Len);
    Series_appg->clear();
    for(int i=0;i<ppg_Len;i++)
    {
        Series_appg->append(QPointF(i,appg.at(i)));
    }

}
